<?php
class model_register extends framework_base_model {
	/*
	* @desc 更新内部用户信息
	* @param array $data
	* @param int $register
	* @return mix
	**/
	public static function update_Inline_user($data, $sales_id){
		if(!is_array($data) || empty($data)){
			return false;
		}
		$sql1 = "update `weixin_user` set `user_id` = {$sales_id},`user_type` = 1 where `openid` in (select `openid` from `weixin_sales_user` where `sales_id` = {$sales_id})";
		$sql = "update `weixin_sales_user` set ";
		$fileds = "";
		foreach($data as $k => $v){
			$fileds .= "{$k} = '{$v}',";
		}
		$fileds .= trim($fileds, ",");
		$where = " where `sales_id` = {$sales_id}";
		try {
    		$pdo = self::DB()->dbh;
			$pdo->beginTransaction();
			$st1 = $pdo->prepare($sql1);
			$st1->execute();
    		$st = $pdo->prepare($sql.$fileds.$where);
    		$st->execute();
			$pdo->commit();
    		return true;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	}
	/*
	* @desc 更新外部用户信息
	* @param array $data
	* @param int $register
	* @return mix
	**/
	public static function update_Outline_user($data, $register_id){
		if(!is_array($data) || empty($data)){
			return false;
		}
		$sql = "update `weixin_register_user` set ";
		$fileds = "";
		foreach($data as $k => $v){
			$fileds .= "{$k} = '{$v}',";
		}
		$fileds .= trim($fileds, ",");
		$where = " where `register_user_id` = {$register_id}";
		$sql1 = "update `weixin_user` set `user_id` = {$register_id},`user_type` = 2 where `openid` in (select `openid` from `weixin_register_user` where `register_user_id` = {$register_id})";
		try {
			$pdo = self::DB()->dbh;
			$pdo->beginTransaction();
			$st1 = $pdo->prepare($sql1);
			$st1->execute();
    		$st = $pdo->prepare($sql.$fileds.$where);
    		$st->execute();
			$pdo->commit();
    		return true;
    	}catch(Exception $e){
			$pdo->rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	}
    /**
     * 获取内部用户列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function registerInlineList($nextNum = 0,$pagesize = 0,$condition = array()) {
    	$sql = "select `sales_id`,a.`openid`,`sales_name`,a.`sex`,`depart`,`sales_type`,`sales_produce`,`sales_area`,`mobile`,`tel`,`fax`,`office_addr`,a.`raw_add_time`,a.`raw_update_time`,`user_category_id`,`deal_state` from `weixin_sales_user` as a,`weixin_user` as b where a.`openid` = b.`openid` and a.`delete_flag` = 0 ";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['username'])) {
    			$where .= " and a.`sales_name` like '%{$condition['username']}%'";
    		}
			if(isset($condition['mobile'])) {
    			$where .= " and a.`mobile` like '%{$condition['mobile']}%'";
    		}
    	}
    	$where .= " order by a.`raw_add_time` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		
		$sql = "select count(*) from `weixin_sales_user` as a,`weixin_user` as b where a.`openid` = b.`openid` and a.`delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	/**
     * 获取外部用户列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function registerOutlineList($nextNum = 0,$pagesize = 0,$condition = array()) {
    	$sql = "select `register_user_id`,a.`openid`,a.`User_ID`,`User_Account`,`Account_State`,`Account_Group`,`Company_Name`,`Company_Address`,`Company_Head`,`Company_Head_Job`,`Company_Head_Tel`,`Contact_Name`,`Contact_Dep`,`Contact_Job`,`Contact_Mobile`,`Contact_Tel`,`Contact_Fax`,`Contact_Mail`,`Org_Code`,`Org_License_No`,`Org_Capital`,`Org_Business`,`Org_Product`,`Deal_State`,a.`raw_add_time`,a.`raw_update_time`,`user_category_id` from `weixin_register_user` as a,`weixin_user` as b where a.`openid` = b.`openid` and a.`delete_flag` = 0 ";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['username'])) {
    			$where .= " and `User_Account` like '%{$condition['username']}%'";
    		}
			if(isset($condition['mobile'])) {
    			$where .= " and `Contact_Tel` like '%{$condition['mobile']}%'";
    		}
    	}
    	$where .= " order by a.`raw_add_time` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		
		$sql = "select count(*) from `weixin_register_user` as a,`weixin_user` as b where a.`openid` = b.`openid` and a.`delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	/*
	 * @desc 查询内部注册用户信息
	 * @param
	 * @return 
	 **/
	 public static function get_inline_user_info($user_id){
		 $sql = "select `sales_id`,a.`openid`,`sales_name`,a.`sex`,`depart`,`sales_type`,`sales_produce`,`sales_area`,`mobile`,`tel`,`fax`,`office_addr`,a.`raw_add_time`,a.`raw_update_time`,`user_category_id`,`deal_state`,`deal_msg`,a.`user_type`,a.`email` from `weixin_sales_user` as a,`weixin_user` as b where a.`openid` = b.`openid` and a.`delete_flag` = 0 and `sales_id` = :sales_id";
		 $binds = array(':sales_id'=>$user_id);
		 try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds);
    		return $st->fetch();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	 }
	 /*
	 * @desc 查询外部注册用户信息
	 * @param
	 * @return 
	 **/
	 public static function get_outline_user_info($user_id){
		 $sql = "select `register_user_id`,a.`openid`,a.`User_ID`,`User_Account`,`Account_State`,`Account_Group`,`Company_Name`,`Company_Address`,`Company_Head`,`Company_Head_Job`,`Company_Head_Tel`,`Contact_Name`,`Contact_Dep`,`Contact_Job`,`Contact_Mobile`,`Contact_Tel`,`Contact_Fax`,`Contact_Mail`,`Org_Code`,`Org_License_No`,`Org_Capital`,`Org_Business`,`Org_Product`,`Deal_State`,a.`raw_add_time`,a.`raw_update_time`,`user_category_id`,b.`user_type`,`Deal_msg` from `weixin_register_user` as a,`weixin_user` as b where a.`openid` = b.`openid` and a.`delete_flag` = 0 and `register_user_id` = :register_user_id";
		 $binds = array(':register_user_id'=>$user_id);
		 try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds);
    		return $st->fetch();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	 }
	 /*
	 * @desc 外部用户审核
	 * @param array $data
	 * @return mix
	 **/
	 public static function check_user($data){
		 $sql = "update	`weixin_register_user` set `Deal_State` = :Deal_State,`Deal_msg` = :Deal_msg where register_user_id = :register_user_id";
		 $binds = array(':Deal_State'=>$data['Deal_State'], ':Deal_msg'=>$data['Deal_msg'],':register_user_id'=>$data['register_user_id']);
		 $sql1 = "update `weixin_user` set `user_id` = {$data['register_user_id']},`user_type` = 2 where `openid` in (select `openid` from `weixin_register_user` where `register_user_id` = {$data['register_user_id']})";
		 try {
			 $pdo = self::DB()->dbh;
			 $pdo->beginTransaction();
			 $st1 = $pdo->prepare($sql1);
			 $st1->execute();

    		 $st = $pdo->prepare($sql);
    		 $st->execute($binds);
			 $pdo->commit();
    		return true;
    	}catch(Exception $e){
			$pdo->rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	 }

	 /*
	 * @desc 内部用户审核
	 * @param array $data
	 * @return mix
	 **/
	 public static function check_inline_user($data){
		 $sql = "update	`weixin_sales_user` set `deal_state` = :deal_state,`deal_msg` = :deal_msg where sales_id = :sales_id";
		 $binds = array(':deal_state'=>$data['deal_state'], ':deal_msg'=>$data['deal_msg'],':sales_id'=>$data['sales_id']);
		 $sql1 = "update `weixin_user` set `user_id` = {$data['sales_id']},`user_type` = 1 where `openid` in (select `openid` from `weixin_sales_user` where sales_id = {$data['sales_id']})";
		 try {
			 $pdo = self::DB()->dbh;
			 $pdo->beginTransaction();
			 $st1 = $pdo->prepare($sql1);
			 $st1->execute();

    		 $st = $pdo->prepare($sql);
    		 $st->execute($binds);
			 $pdo->commit();
    		return true;
    	}catch(Exception $e){
			$pdo->rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	 }

	 //获取员工股类型
	 public static function get_user_catecogry($parent_id){
		 $sql = "select `user_category_id`,`category_name` from `weixin_user_category` where `parent_id` = :parent_id";
		 try {
			 $pdo = self::DB()->dbh;
    		 $st = $pdo->prepare($sql);
    		 $st->execute(array(':parent_id'=>$parent_id));
    		 return $st->fetchAll();
    	}catch(Exception $e){
			$pdo->rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	 }
}
